package Observer;

import java.util.ArrayList;

public class TestObserver {
    public static void main(String[] args){
        TestOriginObserver();
    }

    public static void TestOriginObserver(){
        ConcreteSubject subject = new ConcreteSubject();
        ConcreteObserver1 observer1 = new ConcreteObserver1();
        ConcreteObserver2 observer2 = new ConcreteObserver2();
        subject.registerObserver(observer1);
        subject.registerObserver(observer2);
        subject.setData("test1", "test2");
    }
}

interface subject{
    void registerObserver(Observer obs);
    void removeObserver(Observer obs);
    void notifyObserver();
}

class ConcreteSubject implements subject{
    ArrayList<Observer> notifyList;

    public void setData(String data1, String data2) {
        this.data1 = data1;
        this.data2 = data2;
        notifyObserver();
    }

    String data1;
    String data2;
    public ConcreteSubject(){
        notifyList = new ArrayList<Observer>();
    }
    @Override
    public void notifyObserver() {
        for(Observer observer : notifyList){
            observer.update(this);
        }
    }

    @Override
    public void removeObserver(Observer obs) {
        notifyList.remove(obs);
    }

    @Override
    public void registerObserver(Observer obs) {
        notifyList.add(obs);
    }
}

interface Observer{
    void update(Object obj);
    void display();
}

class ConcreteObserver1 implements Observer{
    String data1;
    @Override
    public void display() {
        System.out.println("DATA:" + data1);
    }

    @Override
    public void update(Object obj) {
        ConcreteSubject subject = (ConcreteSubject)obj;
        this.data1 = subject.data1;
        display();
    }
}

class ConcreteObserver2 implements Observer{
    String data2;
    @Override
    public void display() {
        System.out.println("DATA:" + data2);
    }

    @Override
    public void update(Object obj) {
        ConcreteSubject subject = (ConcreteSubject)obj;
        this.data2 = subject.data2;
        display();
    }
}
